# -*- coding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.tools.safe_eval import safe_eval
from odoo.exceptions import UserError

# 销售订单明细改变发货准备状态
class sale_order_line_for_picking_state(models.TransientModel):

    _name = 'sale.order.line.for.picking.state'

    @api.multi
    def sale_order_line_for_state(self):
        context = dict(self._context or {})
        active_ids = context.get('active_ids', []) or []
        proxy = self.env['sale.order.line']
        for record in proxy.browse(active_ids):
            if record.state =='done' or record.state =='cancel':
                continue
            record.picking_prepare_done='1'
        return {'type': 'ir.actions.act_window_close'}

    @api.multi
    def sale_order_line_for_unstate(self):
        context = dict(self._context or {})
        active_ids = context.get('active_ids', []) or []
        proxy = self.env['sale.order.line']
        for record in proxy.browse(active_ids):
            if record.state == 'done' or record.state == 'cancel':
                continue
            record.picking_prepare_done = '0'
        return {'type': 'ir.actions.act_window_close'}

# 通过销售订单明细生成生产准备
class sale_order_line_for_prepare(models.TransientModel):

    _name = 'sale.order.line.for.prepare'

    @api.multi
    def sale_order_line_for_prepare(self):
        context = dict(self._context or {})
        active_ids = context.get('active_ids', []) or []
        res_lines = []
        proxy = self.env['sale.order.line']
        line_obj = self.env['sale.mrp.prepare']
        for record in proxy.browse(active_ids):
            if record.state =='draft' or record.state =='sent' or record.state =='cancel':
                continue
            if record.product_uom_qty ==0 or record.prepare_done:
                continue
            bom = self.env['mrp.bom']._bom_find(product=record.product_id,
                                                company_id=record.company_id.id)
            if bom.type == 'normal':
                pro_bom = bom.id
                picking_type=bom.picking_type_id.id
            else:
                pro_bom = False
                picking_type = False
            res_line=line_obj.create({
                'name': record.id,
                'picking_type_id':picking_type,
                'product_id': record.product_id.id,
                'product_qty': record.product_uom_qty,
                'product_uom': record.product_uom.id,
                'product_bom': pro_bom,
                # 'need_date': record.need_date,
                'sale_order_id': record.order_id.id,
                'need_date': record.need_date,
                'ask_for_date':record.ask_for_date,
                'mrp_reply_date':record.mrp_reply_date,
                'partner_id': record.order_partner_id.id,
                'state': 'draft',
                             })
            record.prepare_done=True
        return {'type': 'ir.actions.act_window_close'}

# 通过生产准备生成生产计划
class sale_order_line_for_plan(models.TransientModel):

    _name = 'sale.order.line.for.plan'

    @api.multi
    def sale_order_line_for_plan(self):
        context = dict(self._context or {})
        active_ids = context.get('active_ids', []) or []
        res_lines = []
        proxy = self.env['sale.mrp.prepare']
        plan_obj = self.env['sale.mrp.plan']

        for record in proxy.browse(active_ids):
            if record.state =='done' or record.state =='cancel':
                active_ids.remove(record.id)
                continue
            if record.product_qty ==0:
                active_ids.remove(record.id)
                continue
        if active_ids:
            for record in proxy.browse(active_ids):
                # 合并的算法,是直接合并到草稿狀態的计划.如果沒有草稿狀態計劃,才會新建計劃.
                res_plan=plan_obj.search([('state', '=', 'draft'), ('product_id', '=', record.product_id.id)])
                if res_plan:
                    res_plan[0].product_qty=res_plan[0].product_qty+record.product_qty
                    if res_plan[0].order_id.origin and record.sale_order_id.name:
                        res_plan[0].order_id.origin+=record.sale_order_id.name+';'
                    record.name.plan_id = res_plan[0].id
                    record.state = 'done'
                    record.mrp_plan_id = res_plan[0].id
                else:
                    res_line=plan_obj.create({
                        'picking_type_id':record.picking_type_id.id,
                        'product_id': record.product_id.id,
                        'product_qty': record.product_qty,
                        'priority': record.priority,
                        'product_uom': record.product_uom.id,
                        'product_bom': record.product_bom.id,
                        'date_planned': record.date_planned,
                        'need_date': record.need_date,
                        'sale_order_id': record.sale_order_id.id,
                        'partner_id': record.partner_id.id,
                        'state': 'draft',
                                     })
                    record.state='done'
                    record.name.plan_id=res_line.id
                    record.mrp_plan_id=res_line.id
                    res_lines.append(res_line)
            if res_lines:
                order_obj = self.env['sale.mrp.plan.order']
                order_id = order_obj.create({
                    'origin': ''
                })
                for line in res_lines:
                    line.order_id=order_id.id
        return {'type': 'ir.actions.act_window_close'}
